package Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

import static MySql.MySqlStart.InitDatabaseCreate;
import static MySql.MysqlSelect.MySQL_Checkpassword;

public class  LoginServer extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("用户名:" + name);
        System.out.println("密码:" + password);
        try {
            InitDatabaseCreate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (MySQL_Checkpassword(name,password)==false)
        {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<script language='javascript'>alert('账号或者密码错误');" +
                    "window.location.href='Login.jsp';</script>");
        }
        else
        {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<script language='javascript'>alert('密码正确');" +
                    "window.location.href='index.jsp';</script>");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
